package com.sunline.gateway.utils;


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;

/**
 * @Author: DougLiang
 * @CreateTime: 2024-06-04
 * @Description: AES加解密工具类
 * @Version: 1.0
 */
public class AESUtil {

    private static Cipher enCipher;
    private static Cipher deCipher;

    // AES密钥算法
    private static final String KEY_ALGORITHM = "AES";

    // 加密/解密算法/工作模式/填充方式
    private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    public static void getENCipher(String key) throws Exception{
        if (enCipher==null){
            synchronized (AESUtil.class){
                if (enCipher==null){
                    enCipher = Cipher.getInstance(CIPHER_ALGORITHM);
                    SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), KEY_ALGORITHM);
                    enCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
                }
            }
        }
    }

    public static void getDECipher(String key) throws Exception{
        if (deCipher==null){
            synchronized (AESUtil.class){
                if (deCipher==null){
                    deCipher = Cipher.getInstance(CIPHER_ALGORITHM);
                    SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), KEY_ALGORITHM);
                    deCipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
                }
            }
        }
    }

    // 生成密钥
    public static String generateAESKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
        // 192 and 256 bits may not be available
        keyGenerator.init(128, new SecureRandom());
        SecretKey secretKey = keyGenerator.generateKey();
        return Base64.getEncoder().encodeToString(secretKey.getEncoded());
    }

    // AES加密
    public static String encryptAES(String data, String key) throws Exception {
        getENCipher(key);
        byte[] encrypted = enCipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }

    // AES解密
    public static String decryptAES(String data, String key) throws Exception {
        getDECipher(key);
        byte[] decrypted = deCipher.doFinal(Base64.getDecoder().decode(data));
        return new String(decrypted);
    }

    public static void main(String[] args) throws Exception {
//        String key = generateAESKey();
//        System.out.println("KEY: "+ key);
        String originalText = "Hello World!";
        String encryptedText = encryptAES(originalText, "tZnVoceOQ7KhPEHIxx8J3Q==");
        String decryptedText = decryptAES(encryptedText, "tZnVoceOQ7KhPEHIxx8J3Q==");

        System.out.println("Original Text: " + originalText);
        System.out.println("Encrypted Text: " + encryptedText);
        System.out.println("Decrypted Text: " + decryptedText);
    }
}

